﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class EmployeeWorkRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15681;
        protected GridView gvEmployeeWork;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnPara;
        protected HtmlInputButton htmlbtnReport;
        protected Label lblInvoiceList;
        protected Label lblMsg;
        protected LinkButton lnkbtnPrintEmployeeWork;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnPrintEmployeeWork;
        protected UpdatePanel up1;

        private void BindData2EmployeeWorkList(EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(employeeWorkRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetEmployeeWorkRptDataSet(employeeWorkRptQueryEntity);
            this.gvEmployeeWork.Visible = true;
            base.BindData2GridView(this.gvEmployeeWork, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            employeeWorkRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = employeeWorkRptQueryEntity.CurrentPage;
            employeeWorkRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetEmployeeWorkRptDataSet(employeeWorkRptQueryEntity))
                {
                    base.Export2Excel("EmployeeWorkRpt.xls", "15681", this.gvEmployeeWork, ds);
                }
            }
            finally
            {
                employeeWorkRptQueryEntity.IsGetAll = false;
                employeeWorkRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetEmployeeWorkQueryEntity(employeeWorkRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            employeeWorkRptQueryEntity.CurrentPage = "0";
            this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            employeeWorkRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            employeeWorkRptQueryEntity.EMPLOYEE_ID = "";
            employeeWorkRptQueryEntity.StartDateTime = "";
            employeeWorkRptQueryEntity.EndDateTime = "";
            this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            if (int.Parse(employeeWorkRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                employeeWorkRptQueryEntity.CurrentPage = (int.Parse(employeeWorkRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            if (int.Parse(employeeWorkRptQueryEntity.CurrentPage) > 0)
            {
                employeeWorkRptQueryEntity.CurrentPage = (int.Parse(employeeWorkRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
        }

        private EmployeeWorkRptQueryEntity GetEmployeeWorkQueryEntity()
        {
            return (base.PageQueryEntitySession as EmployeeWorkRptQueryEntity);
        }

        private DataSet GetPrintDataset()
        {
            DataSet ds = null;
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
            employeeWorkRptQueryEntity.IsGetAll = true;
            string currentPageIndex = employeeWorkRptQueryEntity.CurrentPage;
            employeeWorkRptQueryEntity.CurrentPage = "0";
            try
            {
                ds = AnalyticsReportService2ClientProxy.GetInstance().GetEmployeeWorkRptDataSet(employeeWorkRptQueryEntity);
            }
            finally
            {
                employeeWorkRptQueryEntity.IsGetAll = false;
                employeeWorkRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetEmployeeWorkQueryEntity(employeeWorkRptQueryEntity);
            }
            return ds;
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
                employeeWorkRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
            }
        }

        protected void gvEmployeeWork_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15681", this.gvEmployeeWork);
        }

        protected void gvEmployeeWork_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
            }
        }

        protected void gvEmployeeWork_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
                employeeWorkRptQueryEntity.SortField = e.SortExpression;
                if (employeeWorkRptQueryEntity.SortDirection == "ASC")
                {
                    employeeWorkRptQueryEntity.SortDirection = "DESC";
                }
                else if (employeeWorkRptQueryEntity.SortDirection == "DESC")
                {
                    employeeWorkRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnPrintEmployeeWork_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = this.GetPrintDataset();
                if ((ds != null) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.Session["PrintEmployeeWork"] = ds;
                    EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = this.GetEmployeeWorkQueryEntity();
                    string date = string.IsNullOrEmpty(employeeWorkRptQueryEntity.StartDateTime) ? "" : Convert.ToDateTime(employeeWorkRptQueryEntity.StartDateTime).ToString("yyyy-MM-dd");
                    string para = date + " 至 " + (string.IsNullOrEmpty(employeeWorkRptQueryEntity.EndDateTime) ? "" : Convert.ToDateTime(employeeWorkRptQueryEntity.EndDateTime).ToString("yyyy-MM-dd"));
                    this.hdnPara.Value = para;
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>popPrintEmployeeWork();</script>", false);
                }
            }
            catch
            {
            }
        }

        private void LoadData()
        {
            EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity = new EmployeeWorkRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    employeeWorkRptQueryEntity.EMPLOYEE_ID = this.PreviousPage.EmployeeID;
                    employeeWorkRptQueryEntity.StartDateTime = this.PreviousPage.StartDate;
                    employeeWorkRptQueryEntity.EndDateTime = this.PreviousPage.EndDate;
                }
                else
                {
                    employeeWorkRptQueryEntity.EMPLOYEE_ID = "";
                    employeeWorkRptQueryEntity.StartDateTime = "";
                    employeeWorkRptQueryEntity.EndDateTime = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            employeeWorkRptQueryEntity.IsGetAll = false;
            employeeWorkRptQueryEntity.CurrentPage = "0";
            employeeWorkRptQueryEntity.PageSize = this.gvEmployeeWork.PageSize.ToString();
            employeeWorkRptQueryEntity.SortField = " STORE_MAN";
            employeeWorkRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvEmployeeWork.PageSize;
            this.SetEmployeeWorkQueryEntity(employeeWorkRptQueryEntity);
            this.BindData2EmployeeWorkList(employeeWorkRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvEmployeeWork);
                this.LoadData();
            }
        }

        private void SetEmployeeWorkQueryEntity(EmployeeWorkRptQueryEntity employeeWorkRptQueryEntity)
        {
            base.PageQueryEntitySession = employeeWorkRptQueryEntity;
        }

        public EmployeeWorkRptQuery PreviousPage
        {
            get
            {
                return (EmployeeWorkRptQuery) base.PreviousPage;
            }
        }
    }
}

